﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Drawing;

namespace DecisionTree
{
    public static class DBWriter
    {         
        public static byte[] Convolution(byte[] currentImage)
        {
            byte[] rVal = new byte[currentImage.Length];

            int imageDim = 28;

            int[,] Vertical = { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } };
            int[,] Horizontal = { { -1, -1, -1 }, { 0, 0, 0 }, { 1, 1, 1 } };

            int[,] conv;

            for (int i = 1; i < imageDim - 1; i++)
            {
                for (int j = 1; j < imageDim - 1; j++)
                {
                    //conv = i % 2 == 0 ? Horizontal : Vertical;
                    conv = Vertical;

                    int sum = 0;
                    for (int m = -1; m <= 1; m++)
                    {
                        for (int n = -1; n <= 1; n++)
                        {
                            sum += currentImage.GetPixel(i + m, j + n) * conv[m + 1, n + 1];
                        }
                    }
                    sum += 3;
                    rVal.SetPixel(i, j, (byte)sum);
                }
            }

            return rVal;
        }

        public static byte[][] Convolution(byte[][] db)
        { 
            byte[][] rVal = new byte[db.Length][];            

            for (int i = 0; i < db.Length; i++)
            {
                rVal[i] = Convolution(db[i]);
            }

            return rVal;
        }
        
        public static byte[] Merge(byte[] image)
        {
            byte[] convoluted = Convolution(image);

            byte[] rVal = new byte[image.Length + convoluted.Length];

            for (int i = 0; i < image.Length; i++)
            {
                rVal[i] = image[i];
            }
            for (int i = 0; i < convoluted.Length; i++)
            {
                rVal[i + image.Length] = convoluted[i];
            }
            return rVal;
        }

        public static byte[][] Merge(byte[][] db) 
        {
            byte[][] rVal = new byte[db.Length][];

            for (int i = 0; i < db.Length; i++)
            {
                rVal[i] = Merge(db[i]);
            }

            return rVal;
        }

        public static void WriteDB(byte[][] db, string path)
        {
            using (BinaryWriter b = new BinaryWriter(File.Open(path, FileMode.Create)))
            {
                int magicNumber = 0;
                int numItems = db.Length;
                int numRows = 28;
                int numCols = 56;

                b.Write(IPAddress.NetworkToHostOrder(magicNumber));
                b.Write(IPAddress.NetworkToHostOrder(numItems));
                b.Write(IPAddress.NetworkToHostOrder(numRows));
                b.Write(IPAddress.NetworkToHostOrder(numCols));                

                for (int i = 0; i < db.Length; i++)
                    for (int j = 0; j < db[i].Length; j++)
                        b.Write(db[i][j]);     
            }
        }
    }
}
